/**
 * @Author: jichang
 * @Date: 2024/10/30 13:34:56
 * 
 * 强制访问顺序
 */

#include <vector>
#include <atomic>
#include <iostream>
#include <thread>

std::vector<int> data;
std::atomic<bool> data_ready(false);

void reader_thread() {
    // 确保访问顺序被严格遵守
    while (!data_ready.load()) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
    std::cout << "the answer = " << data[0] << "\n";
}

void writer_thread() {
    data.push_back(42);
    data_ready = true;
}

int main(int argc, char const *argv[])
{
    std::thread t1(reader_thread);
    std::thread t2(writer_thread);

    t1.join();
    t2.join();
    
    return 0;
}


